package com.correo.vista.client.windows;

import java.awt.BorderLayout;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import com.correo.database.beans.UserBean;
import com.correo.database.client.ConfiguracionClient;
import com.correo.modelo.client.ModelClient;
import com.correo.vista.client.windows.componentes.menu.ItemMenuDinamicos;
import com.correo.vista.client.windows.componentes.menu.MenuCorreoView;
import com.correo.vista.client.windows.componentes.paneles.PanelPrincipal;
import com.correo.vista.client.windows.componentes.paneles.PanelStatus;
import com.correo.vista.client.windows.componentes.paneles.PanelToolBar;
import com.correo.vista.client.windows.ventanasInternas.InternalFrameImpl;

public class Correo extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel 				jContentPane;

	private PanelToolBar 		panelToolBar;
	private PanelPrincipal 		panelPrincipal;
	private PanelStatus 		panelStatus;
	private MenuCorreoView		menuPrincipal;
	private ModelClient 		model;
	private UserBean			userConnect;
	/**
	 * This is the default constructor
	 */
	public Correo(ModelClient model) {
		super();
		this.model = model;
		initialize();
		userConnect = new UserBean();

		userConnect.setId("1");
		ConfiguracionClient.USER="tpjava";
		ConfiguracionClient.PASSWORD="tpjava";
		ConfiguracionClient.URL="jdbc:sqlserver://localhost";
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(615, 461);
		this.setContentPane(getJContentPane());
		this.setTitle("Correo");
		//agregado
		this.setJMenuBar(getMenuPrincipal().getMenuBar());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {				
				System.exit(0);
			}
		});

		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}

	public MenuCorreoView getMenuPrincipal(){
		if(menuPrincipal == null){
			menuPrincipal = new MenuCorreoView(this,model); 
		}
		return menuPrincipal;
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			// createFrame(); //Create first window
			jContentPane.add(getPanelToolBar(),BorderLayout.NORTH);
			jContentPane.add(getPanelPrincipal(),BorderLayout.CENTER);
			jContentPane.add(getPanelStatus(),BorderLayout.SOUTH);
			//setPanels();			
		}
		return jContentPane;
	}

    public void createFrame() {
        InternalFrameImpl frame = new InternalFrameImpl("test",true,true,true,true);
        getPanelPrincipal().add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e2) {}
    }


	/**
	 * This method initializes panelToolBar	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JToolBar getPanelToolBar() {
		if (panelToolBar == null) {
			panelToolBar = new PanelToolBar(getJContentPane());
			//panelToolBar.setBorder(BorderFactory.createLineBorder(Color.magenta, 5));
		}
		return panelToolBar.getToolBar();
	}

	/**
	 * This method initializes panelPrincipal	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JDesktopPane getPanelPrincipal() {
		if (panelPrincipal == null) {
			panelPrincipal = new PanelPrincipal();
			//panelPrincipal.setBackground(Color.BLUE);
		}
		return panelPrincipal.getPanelPrincipal();
	}

	/**
	 * This method initializes panelStatus	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getPanelStatus() {
		if (panelStatus == null) {
			panelStatus = new PanelStatus();
		}		
		//panelStatus.setBackground(Color.BLUE);
		return panelStatus.getStatusPanel();
	}
	public PanelStatus getStatus() {
		if (panelStatus == null) {
			panelStatus = new PanelStatus();
		}		
		//panelStatus.setBackground(Color.BLUE);
		return panelStatus;
	}
	
	public void setTextStatus(String mensaje){
		getStatus().setTextStatus(mensaje);
	}
	public void addItemMenu(ItemMenuDinamicos item){
		getMenuPrincipal().addItemMenu(item);
	}
	public void removeItemMenu(ItemMenuDinamicos item){
		getMenuPrincipal().removeItemMenu(item);
	}

	public ItemMenuDinamicos getSelectMenu() {
		
		return getMenuPrincipal().getSelectMenu();
	}

	public void organizarIconos() {
		int count = getPanelPrincipal().getAllFrames().length;
		for(int i=0;i<count;i++){
			try {
//				 f.setMaximum(true);
//				 f.setSelected(true); 
				
				getPanelPrincipal().getAllFrames()[0].setIcon(true);
			} catch (PropertyVetoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public void organizarMosaico() {
		int cantIntFrames;//cantidad de internalframes
		float cant;	//cantidad de internal frames por fila
		int ancho;//ancho de cada internal frame
		int alto;//alto de cada internalframe
		int columnas;//cantidad de columnas
		int indexFrame=0;//indice del vector de internalframes
		float	x;//posicion x
		float y;//posicion y
		
		cantIntFrames=getPanelPrincipal().getAllFrames().length;
		cant=((float)cantIntFrames)/2;//cantidad de columnas por filas
		columnas = Math.round(cant);
		ancho = (int)getPanelPrincipal().getSize().getWidth()/columnas;
		alto = (int)getPanelPrincipal().getSize().getHeight()/2;
		try {
			setstatesNormal();
			for(int i=0;i<2;i++){
				
				for(int j=0;j<columnas;j++){
					x=ancho*j;
					y=alto*i;
					if(indexFrame<cantIntFrames){
				    	if(getPanelPrincipal().getAllFrames()[i].isIcon()){
							getPanelPrincipal().getAllFrames()[i].setIcon(false);
				    	}
						getPanelPrincipal().getAllFrames()[indexFrame].setBounds(Math.round(x), Math.round(y), ancho, alto);
					}
					indexFrame++;
				}
			}
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(" cantidad de frames por fila : "+Math.round(cant));
		
	}

	public void organizarCascada() {
		int openFrameCount = 0;
	    int xOffset = 30;
	    int yOffset = 30;
	    int cantFrames;
	    cantFrames = getPanelPrincipal().getAllFrames().length;
	    setstatesNormal();
	    try {
		    for(int i=0;i<cantFrames ;i++){
		    	if(getPanelPrincipal().getAllFrames()[i].isIcon()){
					getPanelPrincipal().getAllFrames()[i].setIcon(false);
		    	}
		    	getPanelPrincipal().getAllFrames()[i].setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
				getPanelPrincipal().getAllFrames()[i].setSelected(true);
		    	openFrameCount++;
		    }
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
        
	}
	private void setstatesNormal(){
		int cantFrames = getPanelPrincipal().getAllFrames().length;
		 try {
			    for(int i=0;i<cantFrames ;i++){
			    	if(getPanelPrincipal().getAllFrames()[i].isIcon()){
						getPanelPrincipal().getAllFrames()[i].setIcon(false);
			    	}			    	
			    }
			} catch (PropertyVetoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
	}

	public UserBean getUserConnect() {
		// TODO Auto-generated method stub
		return userConnect;
	}

	public ModelClient getModelClient() {
		// TODO Auto-generated method stub
		return this.model;
	}

	public void setUserConnect(UserBean userConnect) {
		this.userConnect =userConnect; 
		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
